import { _decorator, BoxCollider2D, CircleCollider2D, Collider2D, color, Color, Component, director, EventTouch, find, geometry, Input, instantiate, math, Node, PhysicsSystem, PhysicsSystem2D, PolygonCollider2D, Rect, RigidBody2D, Sprite, tween, UITransform, v2, Vec2, Vec3 } from 'cc';
import { ElementAction } from './ElementAction';
import { LayerRootAction } from './LayerRootAction';
import { Clips, events } from './Enums';
import { AudioMgr } from './AudioMgr';
import { LayerCatAction } from './LayerCatAction';
import { Global } from './Global';
const { ccclass, property } = _decorator;

@ccclass('PinAction')
export class PinAction extends Component {
    pin_color = null;

    rig :RigidBody2D = null;
    img_sprite:Sprite = null;

    onLoad() {
        this.img_sprite = this.node.getChildByName("battery_img").getComponent(Sprite);
        this.rig = this.node.getComponent(RigidBody2D);
        
    }

    start() {

        //7.3
        //this.node.on(Input.EventType.TOUCH_START, this.touch_start, this);

        director.on(events.game_over, this.game_over, this);


        //this.onControlSize();
        
    }


    onControlSize(){

        //----------------------配置倾斜角度(远近的电池大小不同，远的小近的大)----------------------
        let uit = this.node.getChildByName("battery_img").getComponent(UITransform);
        let csize = uit.contentSize;
        let m_dis = this.node.getWorldPosition().y;
        
        //console.log(m_dis)
        let _num = v2(1,1);
        let bnum = m_dis / 580 ;
        
        let cnum = 3.5;
        let dnum = 7;

        //console.log(sf)

        if(m_dis < 580){
            _num.x = 1 - ((1 - bnum)/dnum)
            _num.y = 1 - ((1 - bnum)/cnum)
            //csize.set(csize.width * 0.9 , csize.height * 0.9);

        }
        else if(m_dis > 580){
            _num.x = (bnum - 1)/dnum + 1
            _num.y = (bnum - 1)/cnum + 1
            //csize.set(csize.width * 1.1, csize.height * 1.1);
        }
        
        csize.set(csize.width * _num.x  , csize.height * _num.y);
        //--------------------------------------------------------
    }

    // update(deltaTime: number) {

    // }


    public init_date(group_id: number, pin_color: any) {
        this.rig.group = group_id;
        this.pin_color = pin_color;

        this.node.getComponent(CircleCollider2D).group = group_id;

        //set color
        this.reset_img();
    }
    

    touch_start(e: EventTouch) {
        let eleAct = this.node.parent.getComponent(ElementAction)
        console.log(eleAct)
        eleAct.enabled_rig();

        console.log("点击 pin")
        AudioMgr.ins.playSound(Clips.open_screw);
        // ret?boolean ,return put result,true=success,false=failure

        let b = Global.layer_cat_action.put_pin(this);
        
        //console.log("旋转：",this.node.getRotation())        
        //this.node.setWorldRotation(0,0,0,1);

        //判断放入电池仓
        if (b) {
            // 需要检查，关卡是否结束
            return;
        }

        //放入电池到下面孔
        Global.layer_empty_action.put_pin(this);

        if (Global.layer_empty_action.is_pin_full()) {
            //TODO 游戏结束,这里有个bug,用延迟处理，后面处理

            tween(this.node).delay(0.75).call(() => {
                if (Global.layer_empty_action.is_pin_full()) {

                    //下面电池仓满了，游戏结束
                    //director.emit(events.game_over)
                    
                    Global.game_revive.on_open();
                } else {
                    console.log("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
                }
            }).start();
        }

        
    }


    game_over() {
        this.node.off(Input.EventType.TOUCH_START);
    }

    public remove_collider() {
        this.rig.enabled = false;
        this.node.getComponent(CircleCollider2D).enabled = false;
        this.node.off(Input.EventType.TOUCH_START);

        //新移除碰撞器
        let battery_img = this.node.getChildByName("battery_img");
        battery_img.getComponent(RigidBody2D).enabled = false;
        battery_img.getComponent(Collider2D).enabled = false;
    }

    reset_img() {
        if (!this.pin_color) {
            return;
        }
        
        // console.log("钉子颜色：",this.pin_color)
        // img.color = color(this.pin_color.color)

        //DataModel 中 id 从 10001 开始 ，所有 indexSpr = id - 10001
        let indexSpr = this.pin_color.id - 10001;
        this.img_sprite.spriteFrame = Global.dcImgs[indexSpr]

        let holering = this.node.getChildByName("holering")
        if (holering) {
            tween(holering)
                .to(0.35, { scale: new Vec3(1.2, 1.2, 1) }, { easing: 'quadInOut' })
                .to(0.35, { scale: new Vec3(0.8, 0.8, 1) }, { easing: 'quadInOut' })
                .to(0.25, { scale: new Vec3(1, 1, 1) }, { easing: 'quadInOut' })
                .call(() => {
                    holering.scale = new Vec3(1, 1, 1);
                })
                .start();
        }

    }

    onShow(b){
        this.img_sprite.enabled = b;
    }

    // get_pin_color(arr:ColorDate[]){
    //     if(!this.pin_color){
    //         return;
    //     }
    //     arr.push(this.pin_color);
    // }



}

